 import Vue from "vue"
import Router from 'vue-router'

// 页面
import Login from './view/Login'
import Admin from './view/Admin'
import Index from './components/Index'
import System from './components/System'
import AricleList from './components/article/ArticleList'
import AricleAdd from './components/article/ArticleAdd'
import Platform from './components/platform/Platform'
import Picture from './components/Picture'
import User from './components/User'
import WebSSH from './components/WebSSH'
import ThreeErr from './view/404'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
        path: '/',
        redirect: '/login'
    }, {
        path: '/login',
        name: 'login',
        component: Login
    }, {
        path: '/admin',
        name: 'admin',
        redirect: '/index',
        component: Admin,
        children: [{
            path: '/index',
            name: 'index',
            component: Index
        }, {
            path: '/system',
            name: 'system',
            component: System
        }, {
            path: '/article/list',
            name: 'list',
            component: AricleList
        }, {
            path: '/article/add',
            name: 'add',
            component: AricleAdd
        }, {
            path: '/platform',
            name: 'platform',
            component: Platform
        }, {
            path: '/picture',
            name: 'picture',
            component: Picture
        }, {
            path: '/user',
            name: 'name',
            component: User
        }, {
            path: '/ssh',
            name: 'ssh',
            component: WebSSH
        }]
    }, {
        path: '*',
        component: ThreeErr
    }]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = window.sessionStorage.getItem('name')
    if (to.path === '/login') return next()
    if (!token) return next('/login')
    next()
})

export default router